package com.kx.service_exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kx.service_exam.common.DateFormat;
import com.kx.service_exam.dao.ExamModuleMapper;
import com.kx.service_exam.dao.ExamModuleMapper_My;
import com.kx.service_exam.model.ExamModule;
import com.kx.service_exam.model.ExamModuleExample;
import com.kx.service_exam.model.ExamModuleTemplate;
import com.kx.service_exam.service.interfaces.IExamModuleSV;
import com.kx.service_exam.vo.query.ExamModuleQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ExamModuleSVImpl implements IExamModuleSV{

    @Autowired
    private ExamModuleMapper examModuleMapper;

    @Autowired
    private ExamModuleMapper_My examModuleMapper_my;

    @Override
    public int insertExamModule(ExamModule examModule) {
        Date date = new Date();
        examModule.setId(UUID.randomUUID().toString());
        examModule.setCreate_time(date);
        examModule.setModify_time(date);
        return examModuleMapper.insert(examModule);
    }

    @Override
    public int logicalDeleteExamModule(ExamModule examModule) {
        Date date = new Date();
        examModule.setDelete_flag(1);
        examModule.setModify_time(date);
        return examModuleMapper.updateByPrimaryKeySelective(examModule);
    }

    @Override
    public int deleteExamModule(String id) {
        return examModuleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateExamModule(ExamModule examModule) {
        examModule.setModify_time(new Date());
        return examModuleMapper.updateByPrimaryKeySelective(examModule);
    }

    @Override
    public ExamModule selectExamModuleById(String id) {
        return examModuleMapper.selectByPrimaryKey(id);
    }

    @Override
    public long countExamModuleByFilter(ExamModuleQueryVO filter) throws ParseException {
        return examModuleMapper.countByExample(this.convertFilterToExample(filter));
    }

    @Override
    public List<ExamModule> listExamModuleByFilter(ExamModuleQueryVO filter) throws ParseException {
        return examModuleMapper.selectByExample(this.convertFilterToExample(filter));
    }

    @Override
    public PageInfo<ExamModule> pageExamModuleByFilter(ExamModuleQueryVO filter) throws ParseException {
        if(filter !=null) {
            if(filter.getPage_index() != null && filter.getPage_size() != null){
                PageHelper.startPage(filter.getPage_index(), filter.getPage_size());
            }
        }
        List<ExamModule> list = examModuleMapper.selectByExample(this.convertFilterToExample(filter));
        return new PageInfo<ExamModule>(list);
    }

    @Override
    public List<ExamModuleTemplate> listExamModuleTemplate() {
        return examModuleMapper_my.selectExamModuleTemplate();
    }

    private ExamModuleExample convertFilterToExample(ExamModuleQueryVO filter) throws ParseException {
        if(filter == null) {
            return null;
        }
        ExamModuleExample examModuleExample = new ExamModuleExample();
        ExamModuleExample.Criteria criteria = examModuleExample.or();
        //创建者
        if(!StringUtils.isEmpty(filter.getCreator())) {
            criteria.andCreatorEqualTo(filter.getCreator());
        }
        //起始创建时间
        if(!StringUtils.isEmpty(filter.getCreate_time_start())) {
            //String转为Date类型
            Date create_time_start = new SimpleDateFormat(DateFormat.DATE_TIME_FORMAT.getFormat())
                    .parse(filter.getCreate_time_start());
            criteria.andCreate_timeGreaterThanOrEqualTo(create_time_start);
        }
        //结束创建时间
        if(!StringUtils.isEmpty(filter.getCreate_time_end())) {
            Date create_time_end = new SimpleDateFormat(DateFormat.DATE_TIME_FORMAT.getFormat())
                    .parse(filter.getCreate_time_end());
            criteria.andCreate_timeLessThanOrEqualTo(create_time_end);
        }
        //是否删除
        if(!StringUtils.isEmpty(filter.getDelete_flag())) {
            criteria.andDelete_flagEqualTo(filter.getDelete_flag());
        }
        //评论
        if(!StringUtils.isEmpty(filter.getDescription())) {
            criteria.andDescriptionLike(filter.getDescription());
        }
        //模块名称
        if(!StringUtils.isEmpty(filter.getName())) {
            criteria.andNameLike(filter.getName());
        }
        //父节点
        if(!StringUtils.isEmpty(filter.getParent_id())) {
            criteria.andParent_idEqualTo(filter.getParent_id());
        }
        return examModuleExample;
    }
}
